﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Reporting.WebForms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Data;
using DeviceManager.Models;
using DeviceManager.Filter;
using DeviceManager.Util;
using System.Diagnostics;

namespace DeviceManager.Controllers
{
    public class ReportController : Controller
    {
        //
        // GET: /Report/
        public ActionResult SilencePringPdf()
        {
            string pdfPath = Server.MapPath("/" + DateTime.Now.ToFileTimeUtc() + ".pdf");
            FileStream fs = new FileStream(pdfPath, FileMode.OpenOrCreate);
            byte[] buf;
            string mimeType;
            BuildPdf(out buf, out mimeType);
            MemoryStream m = new MemoryStream(buf);

            m.WriteTo(fs);
            m.Close();
            fs.Close();
            m = null;
            fs = null;
            PrintDocument pd = new PrintDocument();
            Process processInstance = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.Verb = "Print";
            startInfo.CreateNoWindow = true;
            //startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            string arg = @"/p /h \" + pdfPath + "\" \"" + pd.PrinterSettings.PrinterName + " \"";//pd.PrinterSettings.PrinterName;   
            startInfo.Arguments = arg;

            startInfo.FileName = pdfPath;
            processInstance.StartInfo = startInfo;

            processInstance.Start();
            processInstance.WaitForExit();
            System.IO.File.Delete(pdfPath);
            return View();
            
        }

        public ActionResult DownloadPdf() {
            byte[] buf;
            string mimeType;
            BuildPdf(out buf, out mimeType);
            Response.Clear();
            Response.ContentType = mimeType;
            Response.AddHeader("content-disposition", "attachment; filename=foo.pdf");
            Response.BinaryWrite(buf);
            Response.End();
            return View();
        }

        public ActionResult ViewPdf() {
            byte[] buf;
            string mimeType;
            BuildPdf(out buf, out mimeType);
            return File(buf, mimeType);  
        }

        public void BuildPdf(out byte[] b, out string mimeType)
        {
            
            LocalReport localReport = new LocalReport();
            string rdlcPath = Server.MapPath("~/Report/Test.rdlc");
            localReport.ReportPath = rdlcPath;
            IList<ReportData> list = new List<ReportData>();
            ReportData rd;
            for (int i = 0; i < 5; i++)
            {
                rd = new ReportData();
                rd.Id = "id" + i;
                rd.Name = "Name" + i;
                list.Add(rd);
            }

            ReportDataSource reportDataSource = new ReportDataSource("ReportData", list);
            localReport.DataSources.Add(reportDataSource);
            rd = new ReportData();
            list = new List<ReportData>();
            rd.Id = "test";
            rd.Name = "test";
            list.Add(rd);
            reportDataSource = new ReportDataSource("ReportData1", list);
            localReport.DataSources.Add(reportDataSource);
            string reportType = "PDF";
            string encoding;
            string fileNameExtension;
            //The DeviceInfo settings should be changed based on the reportType   
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx   
            string deviceInfo = "<DeviceInfo><OutputFormat>PDF</OutputFormat></DeviceInfo>"; Warning[] warnings; string[] streams;
            //Render the report   

            b = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
            
        }

        public ActionResult Excel() {
            IList<ReportData> list = new List<ReportData>();
            ReportData rd;
            for (int i = 0; i < 5; i++)
            {
                rd = new ReportData();
                rd.Id = "id" + i;
                rd.Name = "Name" + i;
                list.Add(rd);
            }
            Response.AppendHeader("content-disposition", "attachment;filename=" + "test.xls");
            Response.ContentEncoding = Encoding.GetEncoding("gb2312");
            Response.ContentType = "application/excel";
            return File(ExcelUtil.BuildExcel(list), "application/excel");  
            //return new ExportExcelResult(list, "ReportData.xls");
        }
    }
}
